#!/bin/env bash
source ../config
if $(node -v > /dev/null 2>&1)
then
  exit 0
fi
if CheckNetwork
then
  NodeVersion="https://mirrors.ustc.edu.cn/node/latest-iron/"
else
  NodeVersion="https://nodejs.org/dist/latest-iron/"
fi
echo -e ${yellow}正在获取 ${cyan}NodeJS版本号${background}
until NodeVersion="$(curl ${NodeVersion} | grep .pkg | awk -F 'node-|\.pkg' '{print $2}')"
do
  echo -e ${cyan}获取失败 ${yellow}三秒后重试${background}
  sleep 3s
done

if CheckNetwork
then
  NodeJSURL="https://registry.npmmirror.com/-/binary/node/latest-iron/node-${NodeVersion}-linux-$(Arch).tar.xz"
else
  NodeJSURL="https://nodejs.org/dist/latest-iron/node-${NodeVersion}-linux-$(Arch).tar.gz"
fi
if Download NodeJS.tar.xz ${NodeJSURL}
then
  echo -e ${yellow}正在解压 ${cyan}NodeJS.tar.xz${background}
else
  echo -e ${red}解压失败${background}
  ErrorReturn
fi
mkdir NodeJS
pv NodeJS.tar.xz | tar -Jxf - -C NodeJS
rm -rvf NodeJS.tar.xz
rm -rf /usr/local/NodeJS > /dev/null 2>&1
mv NodeJS/$(ls NodeJS) /usr/local/NodeJS
rm -rf NodeJS
echo '
#Node.JS
export PATH=$PATH:/usr/local/NodeJS/bin
' >> /etc/profile
